package com.lingdong.onlinejudge.controller;

import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.sign.IsSignDto;
import com.lingdong.onlinejudge.model.dto.sign.SignDto;
import com.lingdong.onlinejudge.model.dto.sign.SignUpdateDto;
import com.lingdong.onlinejudge.model.entity.Sign;
import com.lingdong.onlinejudge.service.SignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lzw
 */

@RestController
@RequestMapping("/sign")
public class SignController {

    @Resource
    private SignService signService;

    /**
     * 报名
     * @param signDto 报名信息
     * @return 结果
     */
    @PostMapping("/sign")
    public BaseResponse<Boolean> sign(@RequestBody SignDto signDto){
        if (signDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = signService.sign(signDto);
        return ResultUtils.success(result);
    }

    /**
     * 更新报名信息
     * @param signUpdateDto 报名信息
     * @return 结果
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody SignUpdateDto signUpdateDto){
        if (signUpdateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = signService.updateSign(signUpdateDto);
        return ResultUtils.success(result);
    }

    /**
     * 删除报名信息
     * @param id 报名信息 id
     * @return 结果
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> delete(@RequestParam Long id){
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = signService.deleteSign(id);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户是否报名
     * @return
     */
    @PostMapping("/isSign")
    public BaseResponse<Sign> isSign(@RequestBody IsSignDto isSignDto){
        Long userId = isSignDto.getUserId();
        Long matchId = isSignDto.getMatchId();
        if (userId == null || matchId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Sign result = signService.isSign(userId,matchId);
        return ResultUtils.success(result);
    }

}
